package com.wayhome.srpingbootmybatis.test.entity.boc;

import com.wayhome.srpingbootmybatis.utils.XmlUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Data
@ApiModel(value = "中行查询订单返回参数")
public class BocQueryOrdRlstDTO implements IBocResponse{

    @ApiModelProperty(value = "商户订单号", notes = "商户系统产生的订单号")
    private String orderNo;

    @ApiModelProperty(value = "银行订单流水号", notes = "银行的订单流水号（银行产生的订单唯一标识）")
    private String orderSeq;

    @ApiModelProperty(value = "订单状态", notes = "0：未处理 1：支付 2：撤销 3：退货 4：未明 5：失败 6：下单")
    private String orderStatus;

    @ApiModelProperty(value = "银行卡类别", notes = "")
    private String cardTyp;

    @ApiModelProperty(value = "支付时间", notes = "支付交易的日期时间 格式：YYYYMMDDHHMISS")
    private String payTime;

    @ApiModelProperty(value = "支付金额", notes = "支付金额，格式：整数位不前补零,小数位补齐2位 即：不超过10位整数位+1位小数点+2位数 无效格式如123，.10，1.1,有效格式如1.00，0.10")
    private String payAmount;

    @ApiModelProperty(value = "银联订单号", notes = "当使用第三方支付（微信、支付宝）时有返回（此值为该笔订单支付时的银联订单号）")
    private String unionPaySeq;

    public static List<BocQueryOrdRlstDTO> processHttpResult(String httpResult) throws Exception {
        Document document = XmlUtil.createDocument(httpResult);

        String exceptionNode = XmlUtil.getNodeText(document, "exception");
        if (!StringUtils.isBlank(exceptionNode)) {
            throw new Exception("==========>中行支付订单查询结果返回异常信息：" + exceptionNode);
        }
        NodeList orderTrans = document.getElementsByTagName("orderTrans");
        if (orderTrans == null || orderTrans.getLength() <= 0) {
            throw new Exception("==========>中行支付订单查询失败，未查询到支付订单信息，请检查订单号是否传输正确");
        }

        List<BocQueryOrdRlstDTO> list = new ArrayList<>();
        for (int i = 0; i < orderTrans.getLength(); i++) {
            BocQueryOrdRlstDTO bocQueryOrdRlstDTO = new BocQueryOrdRlstDTO();
            Class<? extends BocQueryOrdRlstDTO> clazz = bocQueryOrdRlstDTO.getClass();
            Field[] declaredFields = clazz.getDeclaredFields();
            List<String> fileNames = Arrays.stream(declaredFields).map(Field::getName).collect(Collectors.toList());
            Node item = orderTrans.item(i);
            NodeList childNodes = item.getChildNodes();
            for (int j = 0; j < childNodes.getLength(); j++) {
                Node filedNode = childNodes.item(j);
                String nodeName = filedNode.getNodeName();
                String nodeValue = filedNode.getTextContent();
                if (fileNames.contains(nodeName)) {
                    Field filed = clazz.getDeclaredField(nodeName);
                    filed.setAccessible(true);
                    filed.set(bocQueryOrdRlstDTO, nodeValue);
                }
            }
            list.add(bocQueryOrdRlstDTO);
        }
        return list;
    }

//    @Override
//    public void setFieldByXmlNode(NodeList childNodes, IBocResponse bocQueryOrdRlstDTO) throws Exception {
//        Class<? extends IBocResponse> clazz = bocQueryOrdRlstDTO.getClass();
//        Field[] declaredFields = clazz.getDeclaredFields();
//        List<String> fileNames = Arrays.stream(declaredFields).map(Field::getName).collect(Collectors.toList());
//        for (int j = 0; j < childNodes.getLength(); j++) {
//            Node filedNode = childNodes.item(j);
//            String nodeName = filedNode.getNodeName();
//            String nodeValue = filedNode.getTextContent();
//            if (fileNames.contains(nodeName)) {
//                Field filed = clazz.getDeclaredField(nodeName);
//                filed.setAccessible(true);
//                filed.set(bocQueryOrdRlstDTO, nodeValue);
//            }
//        }
//    }
}
